package com.linln.admin.system.controller;

import com.linln.admin.system.service.AppService;
import com.linln.common.data.QuerySpec;
import com.linln.common.utils.EntityBeanUtil;
import com.linln.common.utils.ValidationUtil;
import com.linln.component.actionLog.annotation.EntityParam;
import com.linln.modules.system.domain.Expert;
import com.linln.modules.system.domain.Manager;
import com.linln.modules.system.domain.Menu;
import com.linln.modules.system.domain.User;
import com.linln.modules.system.repository.ExpertRepository;
import com.linln.modules.system.repository.ManagerRepository;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/system/expert")
public class ExpertController {

    @Autowired
    private ExpertRepository expertRepository;

    @Autowired
    private AppService appService;

    /**
     * 跳转到列表页面
     */
    @GetMapping("/index")
    @RequiresPermissions("system:expert:index")
    public ResponseEntity<?> index(Model model) {

        return appService.layTable(expertRepository, before -> {
                    // 返回模板参数
                    return model;
                },

                after -> {
                    // 列表查询条件
                    QuerySpec querySpec = QuerySpec.matching()
                            .withMatcher("name", QuerySpec.LIKE);
                    Specification<Menu> spec = QuerySpec.of(querySpec);
                    // 返回筛选条件
                    return spec;
                },

                "/system/expert/index",

                (pageResult) -> {
                    // 在这里处理 pageResult
                    // 例如：修改每一条数据
//                    pageResult.getContent().forEach(item -> {
//                        // 对每一项数据进行处理
//                    });
                }

        );

    }



    /**
     * 跳转到添加页面
     */
    @RequestMapping("/form")
    @RequiresPermissions("system:expert:form")
    public ResponseEntity<?> add(Model model, @EntityParam Expert expert) throws Exception {

        return appService.form(expertRepository, "/system/expert/form", (isEdit) -> {

                    return model;

                }, (data) -> {

                    // 保存前数据校验
                    List<ValidationUtil.ValidationRule<?>> rules = new ArrayList<>();

                    // 执行校验
                    return ValidationUtil.validateFields(rules);

                }, (data) -> {
                    if (expert.getId() != null) { // 编辑
                        Expert beExpert = expertRepository.getById(expert.getId());
                        EntityBeanUtil.copyProperties(beExpert, expert); // 将beMenu 字段复制到 menu
                    }
                    return expert;
                },

                (data) -> {
                    // 保存后续逻辑在这写
                });
    }

    /**
     * 修改状态
     * @param model
     * @param expert
     * @return
     * @throws Exception
     */
    @RequestMapping("/state")
    @RequiresPermissions("system:expert:state")
    public ResponseEntity<?> state(Model model, @EntityParam Expert expert) throws Exception {

        return appService.save(expertRepository, (data) -> {

            // 校验数据
            return ValidationUtil.validateFields(new ArrayList<>());

        }, (data) -> {

//            try {
//                throw new Exception("异常");
//            }

            // 处理数据
            Expert origin = expertRepository.getById(expert.getId());
            origin.setStatus(expert.getStatus());
            return origin;

        }, (data) -> {
            // 更新后操作
        });

    }


}
